package com.example.home_form.homework;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 使用JdbcTemplate实现对持久层的操作
 * @author yaojianfeng
 */
@Repository
public class FormInfoDaoImpl implements FormInfoDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public FormInfo queryById(String id) {
        return jdbcTemplate.queryForObject("select * from student_info_20 where student_id = ?", new BeanPropertyRowMapper<>(FormInfo.class),id);
    }

    @Override
    public long count(FormInfo formInfo) {
        return 0;
    }

    @Override
    public int insert(FormInfo formInfo) {
        return 0;
    }

    @Override
    public int update(FormInfo formInfo) {
        return 0;
    }

    @Override
    public int deleteById() {
        return 0;
    }

    @Override
    public int queryCount() {
        return jdbcTemplate.queryForObject("select count(*) from student_info_20",Integer.class);
    }

    @Override
    public List<FormInfo> queryByName(FormInfo info) {
        return jdbcTemplate.query("select * from student_info_20 where chinese_name like ?"
                ,new BeanPropertyRowMapper<>(FormInfo.class)
                ,"%"+info.getChineseName()+"%");
    }

    @Override
    public FormInfo queryOne(FormInfo formInfo) {
        String sql ="select * from student_info_20 where 1=1";
        if (!formInfo.getChineseName().isEmpty()) {
            sql += " and chinese_name = '"+ formInfo.getChineseName()+"'";
        }
        if (!formInfo.getStudentId().isEmpty()) {
            sql += " and student_id = '"+ formInfo.getStudentId()+"'";
        }
        FormInfo formInfo1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(FormInfo.class));
        return formInfo1;
    }
}
